package mobLogBusiness;

import com.alibaba.fastjson.JSONArray;
import models.MyConfig;
import models.ReturnJson;
import models.TestServerBusinessImpl;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Objects;

/**
 * Created by yangxianming on 2023/5/5.
 */
public class MobLogManager implements TestServerBusinessImpl {
    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig) {
        ReturnJson returnJson = new ReturnJson();
        String airTestLogPath = myConfig.getAirTestLogPath();
        JSONArray jsonArray = new JSONArray();
        if (Objects.equals("getLogDateList", request.getParameter("action"))){

            //返回airTestLog下的日期目录列表，排除static目录
            File file = new File(airTestLogPath);
            for (File files: file.listFiles()){
                String fileName = files.getName();
                if (Objects.equals(fileName, "static"))
                    continue;
                jsonArray.add(fileName);
            }
            returnJson.setApiStatus(1);
            returnJson.setResult(jsonArray);
            return returnJson.toString();
        }

        if (Objects.equals("getDateLog", request.getParameter("action"))){
            //返回airTestLog下具体日期下的日志目录列表
            //拿到具体时间
            String date = request.getParameter("date").trim();
            if (Objects.equals(date, "") || Objects.equals(date, null)){
                System.out.println("non date!!!!");
                returnJson.setApiStatus(0);
                return returnJson.toString();
            }
            File file = new File(airTestLogPath + "//" + date);
            if (file.exists()){
                for (File files: file.listFiles()){
                    jsonArray.add(files.getName());
                }
                returnJson.setResult(jsonArray);
                returnJson.setApiStatus(1);
                return returnJson.toString();
            }else {
                System.out.println("non date!!!!");
                returnJson.setApiStatus(0);
                return returnJson.toString();
            }

        }
        returnJson.setApiStatus(0);
        returnJson.setErrorMsg("no action");
        return returnJson.toString();
    }
}
